<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t('基本配置')" :v-if="dialogVisible" :visible.sync="dialogVisible" :width="lang === 'zh'?'480px':'580px'" top="20vh" @close='close' class='ddosDialog'>
    <div class="dialogContentBoxMaxHeight60">
      <el-form ref="form" :model="record" :label-width="lang === 'zh'?'110px':'205px'" :rules="rules" label-position="left" style="margin:0 30px;">
        <el-form-item :label="$t('防御老化时间')" prop="blacktime">
          <el-input v-emoji v-model="record.blacktime" style="width:280px;" size='small' ref="recordBlacktime">
            <template slot="append">
              <div style="width:80px;text-align:center;">{{$t('秒')}}</div>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item :label="$t('检测上报日志周期')" prop="dettime">
          <el-input v-emoji v-model="record.dettime" style="width:280px;" size='small' ref="recordDettime">
            <template slot="append">
              <div style="width:80px;text-align:center;">{{$t('次/分钟')}}</div>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item :label="$t('防御上报日志周期')" prop="deftime">
          <el-input v-emoji v-model="record.deftime" style="width:280px;" size='small' ref="recordDeftime">
            <template slot="append">
              <div style="width:80px;text-align:center;">{{$t('次/分钟')}}</div>
            </template>
          </el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="addSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="addClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getFloodBaseInfo, setFloodBaseInfo } from '@/api/safe/ddos'
import { inputClass } from '@/utils/input-class.js'
export default {
  name: 'DdosDialog',
  data () {
    var validateBlacktime = (rule, value, callback) => {
      if (!String(value).trim()) {
        callback(new Error(this.$t('请输入防御老化时间')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else {
        callback()
      }
    }
    var validateDeftime = (rule, value, callback) => {
      if (!String(value).trim()) {
        callback(new Error(this.$t('请输入防御上报日志周期')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else {
        callback()
      }
    }
    var validateDettime = (rule, value, callback) => {
      if (!String(value).trim()) {
        callback(new Error(this.$t('请输入检测上报日志周期')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      lang: this.$i18n.locale,
      record: {
        blacktime: '',
        deftime: '',
        dettime: ''
      },
      recordCopy: {
        blacktime: '',
        deftime: '',
        dettime: ''
      },
      rules: {
        blacktime: [{ required: false, validator: validateBlacktime, trigger: 'blur' }],
        deftime: [{ required: false, validator: validateDeftime, trigger: 'blur' }],
        dettime: [{ required: false, validator: validateDettime, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init () {
      this.dialogVisible = true
      this.$nextTick(() => {
        const data = ['recordBlacktime', 'recordDettime', 'recordDettime']
        inputClass(this.$refs, data)
      })
      getFloodBaseInfo().then(res => {
        if (res.status === 0) {
          this.record = Object.assign(this.record, res.info[0])
        }
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$alert(this.$t('老化时间的设置只对新建策略生效'), this.$t('提示'), {
            confirmButtonText: this.$t('确定'),
            callback: action => {
              setFloodBaseInfo(this.record).then(res => {
                if (res.status === 0) {
                  this.$message({
                    message: this.$t('配置成功'),
                    type: 'success'
                  })
                  this.close()
                }
              })
            }
          })
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    },
    isInteger (value) {
      return (!isNaN(value) && (parseInt(value).toString().length === String(Number(value)).length))
    }
  }
}
</script>
<style lang="scss" scoped>
.ddosDialog {
  .ivu-switch-checked {
    border-color: #5fb878;
    background-color: #5fb878;
  }
}
</style>
